<template>
    <view class="container">
        <!-- 搜索区域 -->
        <view class="search-section">
            <view class="search-container">
                <view class="search-input-wrapper">
                    <text class="search-icon">🔍</text>
                    <input 
                        class="search-input" 
                        placeholder="搜索活动地点" 
                        placeholder-class="search-placeholder"
                        v-model="searchKeyword" 
                        @input="handleSearchInput"
                        @confirm="handleSearch"
                    />
                    <text 
                        v-if="searchKeyword" 
                        class="clear-icon"
                        @tap="clearSearch"
                    >✕</text>
                </view>
            </view>
        </view>

        <!-- 活动列表 -->
        <scroll-view 
            class="activity-scroll" 
            scroll-y="true" 
            @scrolltolower="loadMore"
            :refresher-enabled="true"
            :refresher-triggered="refreshing"
            @refresherrefresh="onRefresh"
        >
            <view class="activity-list">
                <view 
                    class="activity-card" 
                    v-for="activity in activityList" 
                    :key="activity.id"
                    @click="goToActivityDetail(activity.id)"
                >
                    <image :src="activity.cover" mode="aspectFill" class="activity-image"></image>
                    <view class="activity-content">
                        <view class="activity-title">{{ activity.location }}</view>
                        <view class="activity-description" v-html="activity.description"></view>
                        <view class="activity-info">
                            <view class="info-item">
                                <text class="info-icon">🕐</text>
                                <text class="info-text">{{ formatDateTime(activity.startTime) }} - {{ formatDateTime(activity.expireTime) }}</text>
                            </view>
                            <view class="info-item">
                                <text class="info-icon">📍</text>
                                <text class="info-text">{{ activity.location }}</text>
                            </view>
                            <view class="info-item">
                                <text class="info-icon">👥</text>
                                <text class="info-text">{{ activity.recruitedNum || 0 }}/{{ activity.recruitNum || 0 }}人</text>
                            </view>
                        </view>
                        <!-- <view class="activity-status">
                            <text class="status-text" :class="getStatusClass(activity.registerStatus) || 'status-default'">
                                {{ getStatusText(activity.registerStatus) }}
                            </text>
                        </view> -->
                    </view>
                </view>
            </view>

            <!-- 加载状态 -->
            <view v-if="loading" class="loading-container">
                <text class="loading-text">加载中...</text>
            </view>

            <!-- 无更多数据 -->
            <view v-if="!hasMore && activityList.length > 0" class="no-more">
                <text class="no-more-text">没有更多活动了</text>
            </view>

            <!-- 空状态 -->
            <view v-if="!loading && activityList.length === 0" class="empty-state">
                <image src="/static/images/empty-activity.png" class="empty-image" mode="aspectFit"></image>
                <text class="empty-text">暂无活动</text>
                <text class="empty-desc">换个条件试试吧</text>
            </view>
        </scroll-view>

    </view>
</template>

<script>
export default {
    data() {
        return {
            // 搜索相关
            searchKeyword: '',
            searchTimer: null,
            
            // 列表数据
            activityList: [],
            loading: false,
            refreshing: false,
            hasMore: true,
            pageNo: 1,
            pageSize: 10,
            
        }
    },
    onLoad() {
        this.loadActivityList();
    },
    onUnload() {
        if (this.searchTimer) {
            clearTimeout(this.searchTimer);
        }
    },
    computed: {
        activityListLength() {
            return this.activityList ? this.activityList.length : 0;
        }
    },
    methods: {
        // 搜索输入处理
        handleSearchInput(e) {
            clearTimeout(this.searchTimer);
            this.searchKeyword = e.detail.value;
            this.searchTimer = setTimeout(() => {
                this.performSearch();
            }, 500);
        },

        // 搜索按钮点击
        handleSearch() {
            this.performSearch();
        },

        // 执行搜索
        performSearch() {
            this.pageNo = 1;
            this.activityList = [];
            this.hasMore = true;
            this.loadActivityList();
        },

        // 清空搜索
        clearSearch() {
            this.searchKeyword = '';
            this.performSearch();
        },


        // 加载活动列表
        loadActivityList() {
            if (this.loading) return;
            
            this.loading = true;
            
            const params = {
                pageNo: this.pageNo,
                pageSize: this.pageSize
            };
            
            // 添加搜索条件
            if (this.searchKeyword) {
                params.location = this.searchKeyword;
            }
            
            this.$api.req('/station/activity/page', params, 'GET', 'JSON', false)
                .then(res => {
                    this.loading = false;
                    this.refreshing = false;
                    
                    if (res.code === 0) {
                        const newList = res.data.list || [];
                        
                        if (this.pageNo === 1) {
                            this.activityList = newList;
                        } else {
                            this.activityList = [...this.activityList, ...newList];
                        }
                        
                        this.hasMore = newList.length >= this.pageSize;
                        
                        // 格式化时间显示
                        this.activityList.forEach(activity => {
                            activity.startTimeText = this.formatDateTime(activity.startTime);
                            activity.expireTimeText = this.formatDateTime(activity.expireTime);
                        });
                    } else {
                        uni.showToast({
                            title: res.msg || '加载失败',
                            icon: 'none'
                        });
                    }
                })
                .catch(err => {
                    this.loading = false;
                    this.refreshing = false;
                    console.error('加载活动列表失败:', err);
                    uni.showToast({
                        title: '网络错误',
                        icon: 'none'
                    });
                });
        },

        // 加载更多
        loadMore() {
            if (!this.hasMore || this.loading) return;
            
            this.pageNo++;
            this.loadActivityList();
        },

        // 下拉刷新
        onRefresh() {
            this.refreshing = true;
            this.pageNo = 1;
            this.activityList = [];
            this.loadActivityList();
        },

        // 跳转到活动详情
        goToActivityDetail(id) {
            uni.navigateTo({
                url: `/pages/ActivityDetail/ActivityDetail?id=${id}`
            });
        },

        // 格式化日期时间
        formatDateTime(timestamp) {
            if (!timestamp) return '';
            
            const date = new Date(timestamp);
            const year = date.getFullYear();
            const month = String(date.getMonth() + 1).padStart(2, '0');
            const day = String(date.getDate()).padStart(2, '0');
            const hours = String(date.getHours()).padStart(2, '0');
            const minutes = String(date.getMinutes()).padStart(2, '0');
            
            return `${year}-${month}-${day} ${hours}:${minutes}`;
        },

        // 格式化日期
        formatDate(dateString) {
            if (!dateString) return '';
            
            const date = new Date(dateString);
            const month = date.getMonth() + 1;
            const day = date.getDate();
            
            return `${month}月${day}日`;
        },

        // 获取状态文本
        getStatusText(status) {
            if (status === null || status === undefined) {
                return '未报名';
            }
            const statusMap = {
                0: '未报名',
                1: '待审核',
                2: '已通过',
                3: '已拒绝'
            };
            return statusMap[status] || '未报名';
        },

        // 获取状态样式类
        getStatusClass(status) {
            if (status === null || status === undefined) {
                return 'status-default';
            }
            const classMap = {
                0: 'status-default',
                1: 'status-warning',
                2: 'status-success',
                3: 'status-error'
            };
            return classMap[status] || 'status-default';
        }
    }
}
</script>

<style lang="scss" scoped>
.container {
    display: flex;
    flex-direction: column;
    height: 100vh;
    background: #f5f7fa;
}

/* 搜索区域 */
.search-section {
    background: white;
    padding: 20rpx 30rpx;
    box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
    position: sticky;
    top: 0;
    z-index: 100;
}

.search-container {
    display: flex;
    align-items: center;
}

.search-input-wrapper {
    flex: 1;
    position: relative;
    display: flex;
    align-items: center;
    background: #f8f9fa;
    border-radius: 25rpx;
    padding: 0 20rpx;
    height: 70rpx;
    border: 2rpx solid #e9ecef;
    transition: border-color 0.3s ease;
}

.search-input-wrapper:focus-within {
    border-color: #a55fee;
    box-shadow: 0 0 0 4rpx rgba(165, 95, 238, 0.1);
}

.search-icon {
    margin-right: 10rpx;
    font-size: 32rpx;
    color: #6c757d;
}

.search-input {
    flex: 1;
    font-size: 28rpx;
    color: #333;
    background: transparent;
    border: none;
    outline: none;
}

.search-placeholder {
    color: #adb5bd;
}

.clear-icon {
    margin-left: 10rpx;
    font-size: 28rpx;
    color: #6c757d;
    padding: 10rpx;
    border-radius: 50%;
    transition: background-color 0.2s ease;
}

.clear-icon:active {
    background-color: #e9ecef;
}

/* 活动列表 */
.activity-scroll {
    flex: 1;
    height: calc(100vh - 120rpx);
    background: #f5f7fa;
}

.activity-list {
    padding: 20rpx;
}

.activity-card {
    background: white;
    border-radius: 20rpx;
    margin-bottom: 24rpx;
    overflow: hidden;
    box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);
    transition: all 0.3s ease;
    position: relative;
}

.activity-card:active {
    transform: translateY(2rpx);
    box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.12);
}

.activity-image {
    width: 100%;
    height: 320rpx;
    object-fit: cover;
}

.activity-content {
    padding: 24rpx;
}

.activity-title {
    font-size: 36rpx;
    font-weight: 700;
    color: #2c3e50;
    margin-bottom: 12rpx;
    line-height: 1.3;
}

.activity-description {
    font-size: 28rpx;
    color: #7f8c8d;
    line-height: 1.6;
    margin-bottom: 20rpx;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.activity-info {
    display: flex;
    flex-direction: column;
    gap: 12rpx;
    margin-bottom: 20rpx;
    padding: 16rpx;
    background: #f8f9fa;
    border-radius: 12rpx;
}

.info-item {
    display: flex;
    align-items: center;
    gap: 12rpx;
}

.info-icon {
    font-size: 28rpx;
    margin-right: 8rpx;
}

.info-text {
    font-size: 26rpx;
    color: #495057;
    font-weight: 500;
}

.activity-status {
    display: flex;
    justify-content: flex-end;
    margin-top: 8rpx;
}

.status-text {
    padding: 12rpx 20rpx;
    border-radius: 25rpx;
    font-size: 24rpx;
    font-weight: 600;
    transition: all 0.2s ease;
}

.status-default {
    background: linear-gradient(135deg, #e9ecef, #f8f9fa);
    color: #495057;
    border: 1rpx solid #dee2e6;
}

.status-warning {
    background: linear-gradient(135deg, #fff3cd, #ffeaa7);
    color: #856404;
    border: 1rpx solid #ffd43b;
}

.status-success {
    background: linear-gradient(135deg, #d4edda, #a8e6cf);
    color: #155724;
    border: 1rpx solid #28a745;
}

.status-error {
    background: linear-gradient(135deg, #f8d7da, #ffb3ba);
    color: #721c24;
    border: 1rpx solid #dc3545;
}

/* 加载状态 */
.loading-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 40rpx;
}

.loading-text {
    font-size: 24rpx;
    color: #999;
    margin-top: 10rpx;
}

/* 无更多数据 */
.no-more {
    text-align: center;
    padding: 40rpx;
}

.no-more-text {
    font-size: 24rpx;
    color: #999;
}

/* 空状态 */
.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 100rpx 40rpx;
}

.empty-image {
    width: 200rpx;
    height: 200rpx;
    margin-bottom: 20rpx;
}

.empty-text {
    font-size: 32rpx;
    color: #333;
    margin-bottom: 10rpx;
}

.empty-desc {
    font-size: 24rpx;
    color: #999;
}
</style>
